package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treecol;
import org.zkoss.zul.Treecols;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;
com.xhui.vimarket.zkplusui.vimarket.zkplus.ui.id.ZKRebcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpluscom.xhui.vimarket.zkplusxt;
import com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusan.AttrInst;
import com.xhui.vimcom.xhui.vimarket.zkplusl.attr.bean.AttrTmpl;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCcom.xhui.vimarket.zkplus/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTreeTabComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrTreeTabComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrTreeTabComp inst   = new AttrTreeTabComp();
    
    /**
     * 日志.
     */
    private static Logger          logger = Logger.getLogger(AttrTreeTabComp.class);
    
    /**
     * 构造函数. 
     */
    private AttrTreeTabComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrTreeTabComp instance() {
        return AttrTreeTabComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // tree
        final Tree tree = new Tree();
        tree.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        tree.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        // treecols
        final Treecols tcs = new Treecols();
        final Treecol tc1 = new Treecol();
        tc1.setWidth("80px");
        final Treecol tc2 = new Treecol();
        // treechildren
        final Treechildren tcr = new Treechildren();
        
        // add to zul
        tree.setParent(comp);
        tcs.setParent(tree);
        tc1.setParent(tcs);
        tc2.setParent(tcs);
        tcr.setParent(tree);
        
        // comp log
        if (AttrTreeTabComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<tree>");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerComp("<treecols><treecol width=\"80px\" /><treecol /></treecols>");
        }
        
        // sub tr
        final int sum = attrTmpl.getAttrTmplListCount();
        for (int i = 0; i < sum; i++) {
            final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
            final Treeitem treeitem = new Treeitem();
            ZKRebuildUuid.rebuildUuidById(treeitem, "ti_"
                + (subAttrTmpl.getAttrId() > 0 ? subAttrTmpl.getAttrId()
                    : "_" + Math.abs(subAttrTmpl.getAttrId())));
            treeitem.setParent(tcr);
            final Treerow tr = new Treerow();
            tr.setParent(treeitem);
            final Treecell tcella = new Treecell();
            tcella.setLabel(subAttrTmpl.getName());
            final Treecell tcellb = new Treecell();
            tcella.setParent(tr);
            tcellb.setParent(tr);
            
            // log
            if (AttrTreeTabComp.logger.isDebugEnabled()) {
                attrExt.appendInnerComp("<treechildren><treeitem><treerow><treecell label=\"");
                attrExt.appendInnerComp(subAttrTmpl.getName());
                attrExt.appendInnerComp("\" /><treecell>");
            }
            AttrCompFactory.getInst(subAttrTmpl.getDisplayMode()).create(attrExt, subAttrTmpl,
                tcellb);
            
            // log
            if (AttrTreeTabComp.logger.isDebugEnabled()) {
                attrExt.appendInnerComp("</treecell></treerow></treeitem></treechildren>");
            }
        }
        
        // log
        if (AttrTreeTabComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("</tree>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @SuppressWarnings("unchecked")
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList, final Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList, attrViewMap);
        // 自身不实例化，取下级数据
        // tree//treechildren//treeitem*//treerow//treecell2
        final Component compTreechild = comp.getLastChild();
        final List<Component> treeitemList = compTreechild.getChildren();
        final int sum = treeitemList == null ? 0
            : treeitemList.size();
        AttrTreeTabComp.logger.debug("treeitemList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component treeitem = treeitemList.get(i);
            final Component treerow = treeitem.getFirstChild();
            final Component treecell = treerow.getLastChild();
            final Component panel = treecell.getFirstChild();
            AttrCompFactory.getInstByComponent(panel).setValue(attrExt, panel, attrInstList,
                attrViewMap);
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @SuppressWarnings("unchecked")
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 自身不实例化，取下级数据
        // tree//treechildren//treeitem*//treerow//treecell2
        final Component compTreechild = comp.getLastChild();
        final List<Component> treeitemList = compTreechild.getChildren();
        final int sum = treeitemList == null ? 0
            : treeitemList.size();
        AttrTreeTabComp.logger.debug("treeitemList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component treeitem = treeitemList.get(i);
            final Component treerow = treeitem.getFirstChild();
            final Component treecell = treerow.getLastChild();
            final Component panel = treecell.getFirstChild();
            final ArrayList<AttrInst> subRetList = AttrCompFactory.getInstByComponent(panel)
                .getValue(attrExt, panel);
            if (subRetList != null) {
                retList.addAll(subRetList);
            }
        }
        return retList;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        
        final int sum = attrTmpl.getAttrTmplListCount();
        AttrTreeTabComp.logger.debug("subRowList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
            final AttrInst attrInst = attrExt.newAttrInst();
            attrInst.setAttrId(subAttrTmpl.getAttrId());
            attrInst.setJavaCode(subAttrTmpl.getJavaCode());
            retList.add(attrInst);
        }
        return retList;
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @SuppressWarnings("unchecked")
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrTreeTabComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).clean(attrExt,
                        subCompRootBox);
                }
            }
        }
    }
}
